package com.giw.project.movies;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.parser.HtmlParser;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
import org.xml.sax.SAXException;

public class Prova {

	private String getArticleBody(String link) throws Exception{
		String testo=null;
		Document document=this.getHtmlParsedDocument(link);
		try {
			XPath xpath = XPathFactory.newInstance().newXPath();
			NodeList nodeList = (NodeList) xpath.evaluate("//span[./text()='REGIA: ']/following-sibling::a"
					, document, XPathConstants.NODESET);
			if(nodeList==null){
				throw new Exception("Error trying to evaluate the " +
				"x path expression");
			}
			Element element=(Element) nodeList.item(0);
			if(element ==null){
				return null;
			}
			testo=element.getTextContent();
		}
		catch(XPathExpressionException e){
			throw new Exception(e.getMessage());
		}

		if(testo==null){
			throw new Exception("Error trying to evaluate the " +
			"x path expression");
		}
		System.out.println(testo);
		return testo;
	}

	private Document getHtmlParsedDocument(String link) throws Exception{
		Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING);
		UserAgentContext uacontext = new SimpleUserAgentContext();
		// In this case we will use a standard XML document
		// as opposed to Cobra's HTML DOM implementation.
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document document=null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			URL url = new URL(link);
			InputStream in = url.openConnection().getInputStream();
			Reader reader = new InputStreamReader(in);
			document = builder.newDocument();
			// Here is where we use Cobra's HTML parser.            
			HtmlParser parser = new HtmlParser(uacontext, document);
			parser.parse(reader);
		}
		catch(ParserConfigurationException e){
			throw new Exception(e.getMessage());
		}
		catch(IOException e){
			throw new Exception(e.getMessage());
		}
		catch(SAXException e){
			throw new Exception(e.getMessage());
		}

		if(document==null){
			throw new Exception("Error trying to get the html page of the " +
			"article");
		}

		return document;
	}

	

	public static void main(String args[]) throws Exception{
		Prova p=new Prova();
		p.getArticleBody("http://www.comingsoon.it/Film/Database/?range=3&titoloFilm=&genere=&regiaFilm=&anno=2000&castFilm=&nazionalita=");
	}

}